<?php /* УТФ-8:fix */

class SPIcontroller__adv__front extends SPIcontroller__adv
{
    public function exec_go( & $content )
    {
        $adv_item = SPIm::I('adv')->front->get_by_id( (int) $content['env_vars']['go'] );

        if( $adv_item === false )
        {
            return false;
        }

        if( trim( $adv_item['link'] ) == '' )
        {
            return false;
        }

        SPIm::I('adv')->front->inc_counter( 'click_count', $adv_item['id'] );

        header( 'Location: '.$adv_item['link'] );
        die();
    }

    public function get_advs( $adv_position, $count = 1 )
    {
        $qres = SPIm::I('adv')->front->select( $adv_position, 'random', $count );

        if( $qres === false )
        {
            return false;
        }

        $r = array();

        while( $item = $qres->fetch() )
        {
            if( $item['type'] == 'image' )
            {
                $item['media_item_source'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg( $this->MEDIA_IMAGE_GID, $item['id'], 1 );
            }
            if( $item['type'] == 'flash' )
            {
                $item['media_item_source'] = SPIm::I('media')->get_public_sourceByIndexOld( $this->MEDIA_FLASH_GID, $item['id'], 1 );
            }

            $item['link_goto'] = SPIc::I('front')->link->adv( $item['id'] );
            $r[ $item['id'] ] = $item;
        }
        $qres->close();

        krsort( $r );

        return count( $r ) == 0 ? false : $r;
    }
}
